package com.example.canvasdemo.ui

import android.graphics.Color
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.canvasdemo.databinding.ActivityDrawTestBinding

class DrawTestActivity : AppCompatActivity(), SensorEventListener {
    private lateinit var binding: ActivityDrawTestBinding
    private var sensorManager: SensorManager? = null
    private var lightSensor: Sensor? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDrawTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
        lightSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_LIGHT)

        // 设置画笔颜色
        binding.btnRed.setOnClickListener {
            binding.customCanvas.setColor(Color.RED)
        }
        binding.btnGreen.setOnClickListener {
            binding.customCanvas.setColor(Color.GREEN)
        }
        binding.btnBlue.setOnClickListener {
            binding.customCanvas.setColor(Color.BLUE)
        }
        binding.btnBlack.setOnClickListener {
            binding.customCanvas.setColor(Color.BLACK)
        }

        // 切换到橡皮擦模式
        binding.btnEraser.setOnClickListener {
            binding.customCanvas.enableEraserMode()
        }

        // 恢复到画笔模式
        binding.btnBrush.setOnClickListener {
            binding.customCanvas.disableEraserMode()
        }

        // 调整线条粗细
        binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                binding.customCanvas.setStrokeWidth(progress.toFloat())
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })

        // 清空画布
        binding.btnClear.setOnClickListener {
            binding.customCanvas.clearCanvas()
        }

    }

    override fun onSensorChanged(event: SensorEvent?) {
        if (event != null && event.sensor.type == Sensor.TYPE_LIGHT) {
            val lightLevel = event.values[0]
            // 判断亮度是否低于10
            if (lightLevel < 10) {
                // 弹出提示框
                Toast.makeText(this, "环境亮度过低，请爱护眼睛", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

    }
    override fun onResume() {
        super.onResume()
        // 注册光感传感器
        sensorManager?.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_UI)
    }

    override fun onPause() {
        super.onPause()
        // 注销光感传感器监听
        sensorManager?.unregisterListener(this)
    }

}